<?php

namespace cap;

use think\Config;
use think\Session;

class Cap extends \think\captcha\Captcha
{
    /**
     * @var Config|null
     */
    private $config = null;

    /**
     * @var Session|null
     */
    private $session = null;

    /**
     * 架构方法 设置参数
     * @access public
     * @param Config $config
     * @param Session $session
     */
    public function __construct(Config $config, Session $session)
    {
        $this->config = $config;
        $this->session = $session;
    }

    public  function check(string $code): bool
    {
        if (!$this->session->has('captcha')) {
            return false;
        }
        $captcha  = $this->session->get('captcha');
        $code = mb_strtolower($code, 'UTF-8');
        $res  = password_verify($code, $captcha->key);

        if ($res) {
            $this->session->delete('captcha');
        }

        return $res;
    }
}